/****************************************************************************
 * 2025.8 M777
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class ExpUpgradePanel : UIElement,IController
	{
		private ResLoader mResLoader;
		private void Awake()
		{
			mResLoader = ResLoader.Allocate();
			var iconAtlas = mResLoader.LoadSync<SpriteAtlas>("Icon");
			
			var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();
			
			 foreach (var expUpgradeItem in expUpgradeSystem.Items)
			 {
				 BtnExpUpgradeItemTemplate.InstantiateWithParent(UpgradeRoot)
			 		.Self(self =>
			 		{
			 			var itemCache = expUpgradeItem;

					    self.transform.Find("Icon").GetComponent<Image>().sprite=
						    iconAtlas.GetSprite(itemCache.IconName);
					    
			 			self.onClick.AddListener(() =>
					    {
						    Time.timeScale = 1.0f;
			 				itemCache.Upgrade();
						    this.Hide();
			 				AudioKit.PlaySound("AbilityLevelUp");
			 			});
			 			var selfCache = self;
					    itemCache.Visible.RegisterWithInitValue(visible =>
					    {
						    if (visible)
						    {
							    self.GetComponentInChildren<Text>().text =
								    expUpgradeItem.Description;
							    selfCache.Show();
							    var pairedUpgradeName=selfCache.transform.Find("PairedUpgradeName");
							    if (expUpgradeSystem.Pairs.TryGetValue(itemCache.Key, out var pairedname))
							    {
								    var pairedItem=expUpgradeSystem.Dictionary[pairedname];
								    if (pairedItem.CurrentLevel.Value > 0 && itemCache.CurrentLevel.Value == 0)
								    {
									    pairedUpgradeName.Find("Icon").GetComponent<Image>().sprite=
										    iconAtlas.GetSprite(pairedItem.IconName);
									    pairedUpgradeName.Show();
								    }
								    else
								    {
									    pairedUpgradeName.Hide();
								    }
							    }
							    else
							    {
								    pairedUpgradeName.Hide();
							    }
						    }
						    else
						    {
							    selfCache.Hide();
						    }
					    }).UnRegisterWhenGameObjectDestroyed(selfCache);

					    itemCache.CurrentLevel.RegisterWithInitValue((lv) =>
					    {
						    selfCache.GetComponentInChildren<Text>().text =
							    itemCache.Description;
					    }).UnRegisterWhenGameObjectDestroyed(gameObject);
				    });
			 }
			 
			 


			//升级攻击力按钮事件侦听
			//BtnUpgrade.onClick.AddListener(() =>
			//{
			//Time.timeScale = 1.0f;
			//Global.SimpleAbilityDamage.Value *= 1.5f; 
			//UpgradeRoot.Hide();
			//AudioKit.PlaySound("AbilityLevelUp");
			//});

			//升级攻击距离按钮事件侦听
			//BtnSimpleDurationUpgrade.onClick.AddListener(() =>
			//{
			//Time.timeScale = 1.0f;
			//Global.SimpleAbilityDuration.Value *= 0.8f;
			//UpgradeRoot.Hide();
			//AudioKit.PlaySound("AbilityLevelUp");
			//});
		}
		
		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}